package game.module.mj.processor;

import java.util.ArrayList;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.module.init.logic.RoomConnectManager;
import game.module.mj.bean.MjRound;
import game.module.mj.logic.MjDataManager;
import game.module.mj.logic.MjLogic;
import game.module.user.dao.LanSessionCache;
import lion.common.MsgCodeAnn;
import lion.netty4.message.GamePlayer;
import lion.netty4.message.MyRequestMessage;
import lion.netty4.message.RequestMessageRaw;
import lion.netty4.message.RequestProtoMessage;
import lion.netty4.processor.MsgProcessor;
import lua.LuaMessageMj.C2SMJConnectRoom;
import lua.LuaMessageMj.S2CMJConnectRoom;

/** 
 * 
 * @author llee
 *
 */
@MsgCodeAnn(msgcode = C2SMJConnectRoom.id, accessLimit = 500)
public class MjRoomServerConnectProcessor extends MsgProcessor {

	private static Logger logger = LoggerFactory.getLogger(MjRoomServerConnectProcessor.class);
	@Override
	public void process(GamePlayer gamePlayer, RequestMessageRaw request) throws Exception {
		C2SMJConnectRoom reqMsg = C2SMJConnectRoom.parse(request);
		long roomSessionId = reqMsg.sessionId;
		Integer playerId = LanSessionCache.getInstance().getCachePlayerId(roomSessionId);
		logger.info("Mj room connect,sessionId={},player={}", roomSessionId,playerId);
		if (playerId == null) {
			S2CMJConnectRoom retMsg = new S2CMJConnectRoom(1);
			gamePlayer.writeAndFlush(retMsg.build(gamePlayer.alloc()));
			return;
		}
		// 创建玩家
		RoomConnectManager.cacheNewPlayer(roomSessionId, gamePlayer, playerId);
		S2CMJConnectRoom retMsg = new S2CMJConnectRoom(0);
		gamePlayer.writeAndFlush(retMsg.build(gamePlayer.alloc()));
		//将进入房间的玩家放入对局信息中
		MjRound mjRound = MjDataManager.getInstance().getMjRoom(playerId);
		
		synchronized (mjRound) {
			MjLogic mjlogic = MjLogic.getInstance();
			//logger.info("Mj room connect,sessionId={}",mjRound );
			Set<Integer> enter_player = mjRound.getEnter_player();
			enter_player.add(playerId);
			logger.info("Mj room connect,room munber={},enter_player={}",enter_player.size(),enter_player);
			if(enter_player.size()==4){
				logger.info("mj  start ");
				mjlogic.GameStart(mjRound); //当四个人都连接上房间服务器后，系统开始自动摇色子和自动补花和金牌的确定
			}
		}
	}
	
    
	@Override
	public void process(GamePlayer session, MyRequestMessage request) throws Exception {
		
	}

	@Override
	public void process(GamePlayer player, RequestProtoMessage request) throws Exception {
		
	}
	public static void main(String[] args) {
	    byte[][] cards = new byte[3][7];
		byte[] a =new byte[]{7,8,9,17,18,19,6};
		byte[] b =new byte[]{7,5,4,3,2,12,1};
		byte[] c =new byte[]{8,0,15,2,3,8,10};
		cards[0]=a;
		cards[1]=b;
		cards[2]=c;
		List<Byte> remainCards1 = Arrays.asList((byte)1,(byte)8,(byte)12,(byte)9,(byte)4,(byte)6,(byte)11,(byte)5,(byte)4);
		List<Byte> remainCards = new ArrayList<>(remainCards1);
		List<List<Byte>> huapais = new ArrayList<>();
		List<List<Byte>> bupais = new ArrayList<>();
		MjRound mj = new  MjRound();
	    byte[][] mxj = new	byte [3][42];
	    mj.setCardIndex(mxj);
		  for(byte[] card: cards){
			List<Byte> huapai = new ArrayList<>();
				List<Byte> aplayfilledcard = new ArrayList<>();
				int huapaiCount = 0;
				//找出花牌
				for(int j=0; j<card.length; j++){ 
					if(card[j] >= 65){
						huapai.add(card[j]);
						card[j] = 0;
						huapaiCount++;
						
					}
				}
				//8,0,15,2,3,8,10
				int index = card.length-1;
				for(int m =0; m<index;m++){
					if(card[m]==0){
						while(card[index] ==0){
							index--;
						}
						card[m] = card[index];
						card[index] = 0;
					}
	            }
				//补花
				for(int k =0; k<huapaiCount; k++){
					Byte filled_card = remainCards.get(remainCards.size()-1);
					remainCards.remove(remainCards.size()-1);
		            while(filled_card >= 10){
		            	huapai.add(filled_card);
		            	filled_card = remainCards.get(remainCards.size()-1);
		            	remainCards.remove(remainCards.size()-1);
		            	
		            }
		            card[6-k]=filled_card;
		            
		            //card.add(filled_card);
		            aplayfilledcard.add(filled_card);//一个玩家补到牌的
				}
				huapais.add(huapai);
				bupais.add(aplayfilledcard);
			} 
		  byte [][] index = mj.getCardIndex();
		  int ki = 0;
		  for(byte[] scards:cards ){
			  MjLogic.SwitchToCardIndex(scards, (byte)7, index[ki]);
			  ki++;
		  }
	}
}
